home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1-sources / sources / unix-emulation-src / tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-30  |  3.3 KB  |  175 lines  |  [TEXT/EMAC]

  1. /*
  2.  * Copyright (C) 1993, 1994 Marc Parmet.
  3.  * This file is part of the Macintosh port of GNU Emacs.
  4.  *
  5.  * GNU Emacs is distributed in the hope that it will be useful,
  6.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  8.  * GNU General Public License for more details.
  9.  */
  10.  
  11. #if defined(THINK_C)
  12. #include <MacHeaders>
  13. #else
  14. #include <Types.h>
  15. #include <Memory.h>
  16. #include <Quickdraw.h>
  17. #include <Windows.h>
  18. #include <TextEdit.h>
  19. #include <Resources.h>
  20. #include <Fonts.h>
  21. #endif
  22.  
  23. #include <GestaltEqu.h>
  24. #include "errno.h"
  25. #include "unix-types.h"
  26. #include "unix-constants.h"
  27.  
  28. // Maybe we just need one global pointer to the tty object
  29. // instead of these two.
  30. static WindowPtr w;
  31. static TEHandle teh;
  32.  
  33. void
  34. tty_update(void)
  35. {
  36.     BeginUpdate(w);
  37.     TEUpdate(&w->portRect,teh);
  38.     SetPort(w);
  39.     EndUpdate(w);
  40. }
  41.  
  42. WindowPtr
  43. tty_window(void)
  44. {
  45.     return w;
  46. }
  47.  
  48. void
  49. tty_expose(void)
  50. {
  51.     SelectWindow(w);
  52.     ShowWindow(w);
  53. }
  54.  
  55. void
  56. tty_activate(void)
  57. {
  58. }
  59.  
  60. void
  61. tty_goaway(EventRecord *e)
  62. {
  63.     if (TrackGoAway(w,e->where)) HideWindow(w);
  64. }
  65.  
  66. void
  67. tty_drag(EventRecord *e)
  68. {
  69.     /* WRONG! */
  70.     DragWindow(w,e->where,&qd.screenBits.bounds);
  71. }
  72.  
  73. void
  74. tty_content(void)
  75. {
  76.     SelectWindow(w);
  77. }
  78.  
  79. static long
  80. tty_dispatch(long job,long index,long fd,char *p,long n,struct savearea_lomem *lomem)
  81. {
  82.     GrafPtr gp;
  83.     struct unix_object **object = proctable[index].fd[fd].object;
  84.  
  85.     switch (job) {
  86.     case unix_write:
  87.         if (n > 0) {
  88.             GetPort(&gp);
  89.             if (console_window() != 0L) SelectWindow((**object).u.tty.w);
  90.             ShowWindow((**object).u.tty.w);
  91.             SetPort((**object).u.tty.w);
  92.             unix_translate(p,n);
  93.             TEInsert(p,n,(**object).u.tty.teh);
  94.             unix_translate(p,n);
  95.             TESelView((**object).u.tty.teh);
  96.             SetPort(gp);
  97.         }
  98.         return n;
  99.     case unix_close:
  100.         TEDispose((**object).u.tty.teh);
  101.         DisposeWindow((**object).u.tty.w);
  102.         return 0;
  103.     case unix_read:
  104.         return handle_all_events_but_keystrokes(p,n);
  105.     case unix_fionread:
  106.         *(int *)p = number_of_pending_events();
  107.         return 0;
  108.     }
  109. }
  110.  
  111. static void
  112. init_tty_window(struct unix_object **object)
  113. {
  114.     Rect r;
  115.     void *t;
  116.     int err,have_CQD;
  117.     long response;
  118.     unsigned char *name = (unsigned char *)"\pstdout-stderr";
  119.     extern WindowPtr w;
  120.     extern TEHandle teh;
  121.     
  122.     err = Gestalt(gestaltQuickdrawVersion,&response);
  123.     have_CQD = (err == 0 && response >= gestalt8BitQD);
  124.  
  125.     t = GetResource('WIND',135);
  126.     if (t == 0L) {
  127.         SetRect(&r,40,40,500,240);
  128.         if (have_CQD)
  129.             w = NewCWindow(0L,&r,name,1,noGrowDocProc,(WindowPtr)-1L,1,0L);
  130.         else
  131.             w = NewWindow(0L,&r,name,1,noGrowDocProc,(WindowPtr)-1L,1,0L);
  132.     }
  133.     else
  134.         if (have_CQD)
  135.             w = GetNewCWindow(135,0L,(WindowPtr)-1);
  136.         else
  137.             w = GetNewWindow(135,0L,(WindowPtr)-1);
  138.  
  139.     (**object).u.tty.w = w;
  140.     r = w->portRect;
  141.     OffsetRect(&r,-r.left,-r.top);
  142.     InsetRect(&r,4,2);
  143.     SetPort(w);
  144.     TextFont(monaco);
  145.     TextSize(9);
  146.     teh = TENew(&r,&r);
  147.     (**object).u.tty.teh = teh;
  148.     TEAutoView(-1,teh);
  149. }
  150.  
  151. int
  152. open_internal_dev_tty(void)
  153. {
  154.     int fd;
  155.     struct unix_object **object;
  156.  
  157.     object = lookup_object(object_flavor_dev_tty,0);
  158.     if (object == 0L) {
  159.         object = add_object(object_flavor_dev_tty,0);
  160.         if (object == 0L) { set_errno(EUNDOC); return -1; }
  161.         init_tty_window(object);
  162.     }
  163.     else
  164.         ++(**object).count;
  165.     
  166.     fd = add_fd(fd_flavor_dev_tty,tty_dispatch,object);
  167.     if (fd < 0) {
  168.         dec_object_count(object);
  169.         set_errno(EUNDOC);
  170.         return -1;
  171.     }
  172.     else
  173.         return fd;
  174. }
  175.